package com.karry.yl.service.elder.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.karry.commons.base.impl.SuperServiceImpl;
import com.karry.yl.dao.elder.ElderlyMapper;
import com.karry.yl.dao.elder.FolkMapper;
import com.karry.yl.domain.elder.Elderly;
import com.karry.yl.domain.elder.Folk;
import com.karry.yl.domain.financial.Receipt;
import com.karry.yl.domain.financial.ReceiptItems;
import com.karry.yl.domain.services.Meal;
import com.karry.yl.service.elder.ElderlyService;
import com.karry.yl.service.financial.ReceiptService;
import com.karry.yl.service.services.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class ElderlyServiceImpl extends SuperServiceImpl<ElderlyMapper,Elderly> implements ElderlyService {
    @Autowired
    private FolkMapper folkMapper;
    @Autowired
    private MealService mealService;
    @Autowired
    private ReceiptService receiptService;

    @Transactional
    @Override
    public void save(Elderly elderly) {
        elderly.setPgStatus(1); //待评估
        elderly.setPgjg("待评估");
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);  //当前年份
        int monthNow = cal.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
        cal.setTime(elderly.getBirthday());
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth){age--;}//当前日期在生日之前，年龄减一}
            }else {
                age--;//当前月份在生日之前，年龄减一
            }
        }
        elderly.setAge(age+"");
        baseMapper.insert(elderly);
    }


    @Override
    public List<Folk> selectFamilyList(Long elderlyId) {
        List<Folk> families = folkMapper.selectList(new EntityWrapper<Folk>().eq("elderlyId", elderlyId).eq("deleted",false));
        return families;
    }


    @Override
    @Transactional
    public void setOpenId(String openId,Long elderlyId) {
        //解除其它老人与当前微信的绑定关系
        baseMapper.clearBind(openId);
        baseMapper.setOpenId(openId,elderlyId);
    }

    @Override
    public Long checkOpenId(String openId) {
        Long id = baseMapper.checkOpenId(openId);
        return id;
    }

    @Override
    @Transactional
    public void bindMeal(Long elderlyId, Long tcId, String tcName,Long userId,String orgName) {
        Elderly elderly = this.selectById(elderlyId);
        if(null != tcId && !tcId.equals(elderly.getMealId())){   //避免同一个套餐重复绑定
            if(!tcId.equals(-1L)){
                Meal meal = mealService.selectById(tcId);
                elderly.setMealName(tcName);
                Calendar curr = Calendar.getInstance();
                if(meal!=null){
                    curr.add(Calendar.MONTH,meal.getCycle());  //付款周期
                }
                elderly.setMealEndTime(curr.getTime());  //设置套餐到期时间
                //生成收费单
                Receipt receipt = new Receipt();
                receipt.setOrgId(elderly.getOrgId());
                receipt.setOrgName(orgName);
                receipt.setElderlyId(elderly.getId());
                receipt.setElderlyName(elderly.getName());
                receipt.setName("服务套餐【" + elderly.getMealName() +"】");
                if(meal!=null){
                    receipt.setAmount(meal.getPrice());
                }
                receipt.setStatus(Receipt.STATUS_ZC);
                receipt.setType(Receipt.TYPE_TC);
                receipt.setUserId(userId);
                List<ReceiptItems> receiptItemsList = new ArrayList<>();
                ReceiptItems receiptItem = new ReceiptItems();
                if(meal!=null){
                    receiptItem.setMonery(meal.getPrice());
                    receiptItem.setName(meal.getName());
                }
                receiptItem.setType(ReceiptItems.TYPE_TC);
                receiptItemsList.add(receiptItem);
                receiptService.save(receipt,receiptItemsList);
            }else{
                elderly.setMealName("无");
                elderly.setMealEndTime(null);
            }

            elderly.setMealId(tcId);
            this.updateById(elderly);
        }
    }

    @Override
    public void mealXufei(Long elderlyId,Long userId,String orgName) {
        Elderly elderly = this.selectById(elderlyId);
        Calendar curr = Calendar.getInstance();
        curr.setTime(elderly.getMealEndTime());
        curr.add(Calendar.MONTH,1);
        elderly.setMealEndTime(curr.getTime());  //设置套餐到期时间
        Meal meal = mealService.selectById(elderly.getMealId());
        this.updateById(elderly);
        //生成收费单
        Receipt receipt = new Receipt();
        receipt.setOrgId(elderly.getOrgId());
        receipt.setOrgName(orgName);
        receipt.setElderlyId(elderly.getId());
        receipt.setElderlyName(elderly.getName());
        receipt.setName("套餐收费【" + elderly.getMealName() +"】");
        receipt.setAmount(meal.getPrice());
        receipt.setStatus(Receipt.STATUS_ZC);
        receipt.setType(Receipt.TYPE_TC);
        receipt.setUserId(userId);
        List<ReceiptItems> receiptItemsList = new ArrayList<>();
        ReceiptItems receiptItem = new ReceiptItems();
        receiptItem.setMonery(meal.getPrice());
        receiptItem.setName(meal.getName());
        receiptItem.setType(ReceiptItems.TYPE_TC);
        receiptItemsList.add(receiptItem);
        receiptService.save(receipt,receiptItemsList);
    }


    @Override
    @Transactional
    public void mealXufei(Long[] elderlyId, Long userId,String orgName) {
        for(Long eId : elderlyId){
            this.mealXufei(eId,userId,orgName);
        }
    }
}
